package com.hou.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hou.commonutils.JwtUtils;
import com.hou.commonutils.MD5;
import com.hou.educenter.entity.UcenterMember;
import com.hou.educenter.entity.vo.RegistorVo;
import com.hou.educenter.mapper.UcenterMemberMapper;
import com.hou.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-11-19
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public String login(UcenterMember member) throws Exception {
        //获取登录手机号和密码
        String mobile = member.getMobile();
        String password = member.getPassword();

        //手机号和密码非空判断
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new Exception("登录失败");
        }

        //判断手机号是否正确
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper);
        //判断查询对象是否为空
        if(mobileMember == null) {//没有这个手机号
            throw new Exception("登录失败");
        }

        //判断密码
        //因为存储到数据库密码肯定加密的
        //把输入的密码进行加密，再和数据库密码进行比较
        //加密方式 MD5
        if(!MD5.encrypt(password).equals(mobileMember.getPassword())) {
            throw new Exception("登录失败");
        }

        //判断用户是否禁用
        if(mobileMember.getIsDisabled()) {
            throw new Exception("登录失败");
        }

        //登录成功
        //生成token字符串，使用jwt工具类
        String jwtToken = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());
        return jwtToken;
    }

    // 注册
    @Override
    public Boolean register(RegistorVo registorVo) throws Exception {


        // 验证码
        String code = registorVo.getCode();
        // 手机号
        String mobile = registorVo.getMobile();
        // 用户昵称
        String nickName = registorVo.getNickname();
        // 用户密码
        String password = registorVo.getPassword();

        // 判空操作
        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(mobile) || StringUtils.isEmpty(nickName) || StringUtils.isEmpty(password)) {
            return false;
        }

        // 获取redis中的验证码，并根据此匹配输入的验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!redisCode.equals(code)){
            return false;
        }

        // 查询数据库中是否存在改手机信息
        // 如果存在注册失败
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(queryWrapper);

        if (count > 0) {
            return false;
        }

        // 将注册信息添加到数据库中
        UcenterMember ucenterMember = new UcenterMember();

        BeanUtils.copyProperties(registorVo,ucenterMember);
        // 密码加密
        ucenterMember.setPassword(MD5.encrypt(registorVo.getPassword()));
        // 未禁用
        ucenterMember.setIsDisabled(false);
        // 用户头像
        ucenterMember.setAvatar("https://hou-file-001.oss-cn-beijing.aliyuncs.com/2021/11/16/7b383be532fb40c582ad87dc26113b3601.jpg");


        baseMapper.insert(ucenterMember);

        return true;

    }

    // 查找微信用户
    @Override
    public UcenterMember getUserByOpenId(String openid) {

        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("openid",openid);

        UcenterMember member = baseMapper.selectOne(queryWrapper);

        return member;
    }

    @Override
    public Integer countRegisterDay(String day) {
        return baseMapper.countRegisterDay(day);
    }
}
